package com.px.scserver.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.px.scserver.api.pc.v1.request.JuniorCollegePageRequest;
import com.px.scserver.api.pc.v1.request.JuniorCollegeSaveRequest;
import com.px.scserver.api.pc.v1.vo.ImportJuniorCollegeVO;
import com.px.scserver.api.pc.v1.vo.JuniorCollegeVO;
import com.px.scserver.api.wx.v1.vo.JuniorCollegeInfoVO;
import com.px.scserver.converter.JuniorCollegeConverter;
import com.px.scserver.mapper.JuniorCollegeMapper;
import com.px.scserver.po.JuniorCollege;
import com.px.scserver.service.JuniorCollegeEnrollmentMajorService;
import com.px.scserver.service.JuniorCollegeService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 品讯科技
 * @description 针对表【sc_junior_college】的数据库操作Service实现
 * @createDate 2023-09-05 21:34:11
 */
@Slf4j
@AllArgsConstructor
@Service
public class JuniorCollegeServiceImpl extends ServiceImpl<JuniorCollegeMapper, JuniorCollege>
        implements JuniorCollegeService {

    private final JuniorCollegeConverter converter;
    private final ApplicationContext applicationContext;

    @Override
    public void importData(MultipartFile file) {
        List<ImportJuniorCollegeVO> list = new ArrayList<>();
        try {
            list = EasyExcel.read(file.getInputStream()).head(ImportJuniorCollegeVO.class).sheet().doReadSync();
        } catch (Exception e) {
            log.error("入数据有误！", e);
            throw new RuntimeException("导入数据有误！");
        }

        // saveBatch(converter.converterList(list));
        for (var item : converter.converterList(list)) {
            try {
                save(item);
            } catch (Exception e) {
            }
        }
    }

    @Override
    public IPage<JuniorCollegeVO> page(JuniorCollegePageRequest request) {
        Page<JuniorCollege> page = lambdaQuery()
                .like(StringUtils.isNoneEmpty(request.getJuniorCollegeName()), JuniorCollege::getJuniorCollegeName, request.getJuniorCollegeName())
                .page(new Page<>(request.getCurrent(), request.getSize()));

        return page.convert(converter::converter);
    }

    @Override
    public void saveOrUpdate(JuniorCollegeSaveRequest request) {
        JuniorCollege entity = converter.converter(request);
        if (Objects.isNull(entity.getId())) {
            save(entity);
            return;
        }
        updateById(entity);
    }

    @Override
    public IPage<JuniorCollegeVO> pageWx(JuniorCollegePageRequest request) {
        Page<JuniorCollege> page = lambdaQuery()
                .select(JuniorCollege::getId, JuniorCollege::getJuniorCollegeName, JuniorCollege::getJuniorCollegeCode)
                .like(StringUtils.isNotBlank(request.getJuniorCollegeName()), JuniorCollege::getJuniorCollegeName, request.getJuniorCollegeName())
                .notLike(JuniorCollege::getType, "3")
                .notLike(JuniorCollege::getType, "6")
                .page(new Page<>(request.getCurrent(), request.getSize()));

        return page.convert(converter::converter);
    }

    @Override
    public JuniorCollegeInfoVO getJuniorCollegeInfo(Long id) {

        return applicationContext.getBean(JuniorCollegeEnrollmentMajorService.class).getJuniorCollegeInfo(id);
    }
}




